<template>


  <div>

    <el-row>


      <el-col :span="12">
        <el-button @click="toAdd()">添加</el-button>

      </el-col>
      <!--      <el-col :span="12"><div class="grid-content ep-bg-purple-dark" /></el-col>-->
    </el-row>


    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <!-- <el-table-column prop="itemId" label="itemId" width="180" /> -->


      <el-table-column prop="caption" label="级别名称" />
      <el-table-column prop="fee" label="挂号费用" />

      <el-table-column prop="createtime" label="创建时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="deleteById(scope.row.levelId)">删除</el-button>
          <el-button link type="primary" @click="toEdit(scope.row.levelId)">修改</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination v-model:current-page="queryParam.current" :page-sizes="[3, 5, 10, 15, 20, 30, 40]"
      v-model:page-size="queryParam.size" size="default" layout="sizes, prev, pager, next" :total="total"
      @size-change="loadDataList" @current-change="loadDataList" />



    <el-dialog title="挂号级别管理" width="600px" v-model="showEdit">

      <el-form :model="form" label-width="auto">

        <el-form-item label="名称">
          <el-input v-model="form.caption" />
        </el-form-item>
        <el-form-item label="费用">
          <el-input v-model="form.fee" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button @click="closeDialog()">Cancel</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>


  </div>




</template>


<script lang="ts" setup name="User">

import { ref, onMounted, reactive } from 'vue'
// import { service } from "@/utils/request";
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

import { page, deleteById as del, queryById, saveOrUpdate, type PageInfo } from '@/utils/crud'

interface RegistLevel {
  levelId?: string;
  caption: string;
  fee: number | undefined;
  createtime?: string;
}

const route = useRoute();

//数据




const tableData = ref<RegistLevel[]>()
const queryParam = reactive({
  current: 1,
  size: 10
});
const total = ref(0);
const loading = ref(false);

const form = reactive<RegistLevel>({
  caption: '',
  fee: undefined,

})
const showEdit = ref(false);


function loadDataList() {
  loading.value = true;
  // 为给定 ID 的 user 创建请求
  //data  真实的数据
  page<RegistLevel>('/regist_level/page', queryParam).then(function (page) {
    tableData.value = page.data;
    total.value = page.total;
    loading.value = false;
  })

}

function deleteById(id: string) {
  ElMessageBox.confirm(
    '是否删除数据?',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      del('/regist_level/delete', id).then(function (success) {
        if (success) {
          ElMessage({
            message: '删除成功',
            type: 'success'
          })

          loadDataList();
        } else {
          ElMessage({
            message: '删除失败',
            type: 'error'
          })

        }
      })
    })

}

function save() {

  saveOrUpdate('/regist_level/saveOrUpdate', form).then(function (success) {
    if (success) {
      ElMessage({
        message: '保存成功',
        type: 'success'
      })
      closeDialog();
      loadDataList();
    } else {
      ElMessage({
        message: '保存失败',
        type: 'error'
      })
    }

  })
}

function toAdd() {
  showEdit.value = true
  form.levelId = undefined;
  form.caption = '';
  form.fee = undefined

}
function toEdit(id: string) {


  //根据主键查询 数据 放到  form 表单
  queryById<RegistLevel>('/regist_level/getById', id).then((vo) => {

    showEdit.value = true;

    form.levelId = vo.levelId;
    form.caption = vo.caption;
    form.fee = vo.fee;
  })

}
function closeDialog() {
  showEdit.value = false
}

onMounted(() => {
  // 获取数据
  loadDataList();
})


</script>
